import 'dart:async';

import 'package:base/common/init/config.dart';
import 'package:base/common/init/env.dart';
import 'package:base/common/init/info.dart';
import 'package:base/common/net/dio_interceptors/base_interceptor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:jskit/jskit.dart';

import 'common/router/app_router.dart';
final app = Application();
class Application {
  /// 私有构造函数
  Application._internal() {
    container = ProviderContainer();
    _appRouter = AppRouter();
    // 全局定时器
    _globalTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      timerCounter.value = timerCounter.value + 1;
    });

    _log("init");
  }

  /// 全局单例
  static final Application _singleton = Application._internal();

  /// 工厂构造方法
  factory Application() => _singleton;

  late AppRouter _appRouter;
  AppRouter get appRouter => _appRouter;

  /// 配置信息
  late ProviderContainer container;

  // delivery time event
  Timer? _globalTimer;
  final ValueNotifier<int> _timerCounter = ValueNotifier(0);
  ValueNotifier<int> get timerCounter => _timerCounter;

  // 初始化
  Future<void> init({WidgetsBinding? binding}) async {
    var widgetsBinding = binding ?? WidgetsFlutterBinding.ensureInitialized();

    // 初始化配置
    await Config().init();

    // 获取项目基础信息
    var ret = await BaseInfoHandle().init();

    // 获取当前环境信息
    var ret2 = await EnvHandle().init();

    // 初始化网络请求
    // DioUtil().initNet(baseUrl: "http://192.168.111.90:8108", interceptors: [BaseInterceptor()]);
    DioUtil().initNet(baseUrl: "https://mhapi.sensetime.com", interceptors: [BaseInterceptor()]);
  }

  _log(String message) {
    assert(() {
      print('[Application]: $message');
      return true;
    }());
  }
}
